<?php
/**
 * Mount the Photo Upload form.
 *
 */
class adminGallery extends div
{
    public function __construct()
    {
        global $photo;
        global $page;

        //faz o Upload...
        $uploadedFile = uploadFile( GALLERYPATH , true);
        if ( $uploadedFile != false )
        {
            $page->addJsCode("alert('Imagem $uploadedFile enviado com sucesso!');");
        }
        else
        {
            if ($uploadedFile == "false")
            {
                $page->addJscode('alert(\'Falha na Exclusao do Arquivo!\');');
            }
        }

        $delImgSubmit = new Submit('delImgSubmit', 'Excluir', 'columnTitle');
        $delImgSubmit -> setBounds(290,410,150,30);
        $delImgSubmit -> onClick('delImgSubmitClick');

        $photoSubmit2  = new Submit('photoSubmit2', 'Salvar', 'columnTitle');
        $photoSubmit2  -> setBounds(120,410,150,30);
        $photoSubmit2  -> onClick('photoSubmitClick2');

        $options = listFiles( GALLERYPATH, true);

        $file      = new SelectField('file', $options, 'Arquivo', 100 ,320 , 20 , 20 ,40);
        $file      ->getField()->addEvent('onchange', 'showImg()');

        $page->addJsCode( $photo->getJsPhotoData() );
        $page->addJsCode( showImg() );
        $page->addJsCode("showImg();");

        $title        = new TextField('msg',null, 'Mensagem', 100,320, 20, 20, 75, 'text');

        $label        = new Span('label', 'Imagem');
        $label        ->SetBounds(20,110, 100,20);

        $img          = new img('imgImg', "uploaded/$options[0]");
        $img          ->width = '100%';

        $divImg       = new divMove('divImg', $img, 'text');
        $divImg       ->setBounds(120,110, 320,240);

        $linkSpanLb   = new Span('linksLabel','Link:', 'text');
        $linkSpanLb  ->setBounds(20,370, 450,20);

        $newImgSubmit = new Submit('newImgSubmit', '+', 'columnTitle');
        $newImgSubmit -> SetBounds(450,40,20,20);

        $labelFile    = new Span('labelFile', 'Enviar Novas Imagens');
        $labelFile    -> setBounds(20,20);
        $fileUpload   = new input('fileUpload', 'file');
        $fileUpload   ->setBounds(100,50);
        $uploadSubmit = new Submit('uploadSubmit', 'Enviar Arquivo!', 'columnTitle');
        $uploadSubmit ->setBounds(100,80, 150,30);

        $divUpload    = new div('divUpload', array($labelFile, $fileUpload, $uploadSubmit), 'text');
        $divUpload    -> setBounds(60, 530, 420,120);

        $photoForm    = new form('formPhoto', array(    $divUpload ), 'index.php', 'post', 'return validate_form()');

        $photoForm    ->enctype = 'multipart/form-data';

        if ($_POST['newImgSubmit'])
        {
            $height    = 600;
        }
        else
        {
            $photoForm = null;
            $height    = 460;
        }

        $divPhoto     = new DivForm('divPhoto', array($file, $title, $label, $divImg, $photoSubmit2, $newImgSubmit, $delImgSubmit, $linkSpan, $linkSpanLb),500,$height,"Fotos");
        $divPhoto->getBar()->class = 'columnTitle';
        $divPhoto->class = 'text';

        $uploadForm = new form('formPhotoUpload', array($divPhoto), 'index.php', 'post', 'return validate_form()');

        parent::div( null , array($uploadForm, $photoForm) );
    }
}







function showImg()
{
    return "
function showImg()
{
    var select = document.getElementById('file');
    if (select)
    {
        var value  = photoData[select.value];
        if (value == undefined)  {value = ''; }
        document.getElementById('msg').value = value;
        var imgpath = document.getElementById('file').value;
        var newImage = \"uploaded/\"+imgpath;
        document.getElementById('imgImg').src = newImage;
        document.getElementById('linksLabel').innerHTML = newImage;
    }
}";
}

/**
* On click photoSubmitClick buton.
*
*/
function photoSubmitClick2()
{
    global $page;
    global $photo;

    if ($photo->savePhotoData($_POST) )
    {
        $page ->addJsCode('alert(\Informacoeses salvas com sucesso!\')');
    }
    else
    {
        $page->addJsCode('alert(\'falha na Operacao!\')');
    }
}


/**
* Delete An Image from database and folder, also delete it from database.
*
*/
function delImgSubmitClick()
{
    global $photo;
    global $page;
    $file   = $_POST['file'];
    $result = $photo ->delete( GALLERYPATH , $file);

    if ($result)
    {
        $page->addJsCode("alert('Imagem $file deletada com sucesso!!');");
    }
    else
    {
        $page->addJsCode('alert("Falha na operacao!");');
    }
}
?>